package org.xdq.springcloud.demo20191222.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.xdq.base.Result;
import org.xdq.dto.RoleDto;
import org.xdq.exception.BusinessException;
import org.xdq.exception.SysException;
import org.xdq.model.Module;
import org.xdq.springcloud.demo20191222.service.RoleService;

@RestController
public class RoleController {
	
	@Autowired
	private RoleService roleService;
	
	@GetMapping("/ro")	
	public Map<String,Object> cusList(RoleDto dto){		
		
		return roleService.getRoleList(dto);
	}
	
	@PostMapping("/ro")	
	public Result execAddCus(@RequestBody RoleDto dto){
		roleService.addRole(dto);
		return Result.success("新增角色信息成功！");
	}
	
	@PutMapping("/ro")	
	public Result execUpdCus(@RequestBody RoleDto dto){
		roleService.updateRole(dto);
		return Result.success("修改角色信息成功！");
	}
	
	@DeleteMapping("/ro/{id}")	
	public Result execDelCus(@PathVariable Integer id){
		roleService.deleteRole(id);
		return Result.success("删除角色信息成功！");
	}
	
	@DeleteMapping("/ro")	
	public Result execDelCus(@RequestBody Integer... ids){
		try {
			roleService.deleteRole(ids);
			return Result.success("删除角色信息成功！");
		} catch (BusinessException e) {
			return Result.fail(e.getMessage());
		}catch (SysException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return Result.fail("系统维护中...");
		}
	}
	
	@PutMapping("/{opr}/{id}")
	public Result execOperateStatusRo(@PathVariable("opr") String opr,@PathVariable Integer id) {
			
		try {
			String successInfo = roleService.operateRole(opr,id);
			return Result.success(successInfo);
		} catch (BusinessException e) {
			return Result.fail(e.getMessage());
		}catch (SysException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return Result.fail("系统维护中...");
		}
	}
	
	@PutMapping("/{opr}")
	public Result execOperateStatusRo(@PathVariable String opr,@RequestBody Integer[] id) {
			
		try {
			String successInfo = roleService.operateRole(opr,id);
			return Result.success(successInfo);
		} catch (BusinessException e) {
			return Result.fail(e.getMessage());
		}catch (SysException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return Result.fail("系统维护中...");
		}
	}
	
	@GetMapping("/module")
	public List<Module> getAllModuleList(){
		
		return roleService.getAllModuleList();
		
	}
	
	@GetMapping("/roleModuleId/{ro_id}")
	public List<Integer> getRoleModuleId(@PathVariable Integer ro_id){
		
		return roleService.getRoleModuleId(ro_id);
		
	}
	@PutMapping("/module/{ro_id}")
	public Result confirmRoleModule(@PathVariable Integer ro_id, @RequestBody Integer[] m_ids ) {
		roleService.confirmRoleModule(ro_id,m_ids);
		return Result.success("权限设置成功！");
	}

}
